package meatslope2.constants;

import javax.microedition.khronos.opengles.GL10;

import meatslope2.application.R;
import meatslope2.gameengine.gameobjects.Tile;
import meatslope2.gameengine.helpers.GLHelper;
import meatslope2.gameengine.primatives.LineSegment;
import meatslope2.gameengine.primatives.Vector2;
import android.content.Context;

/**
 * Include index for each static tile to hardcode for level building in code
 * 
 * @author Mitchell Thelen
 *
 */
public class TileConstants 
{
	private static final Tile.Builder builder = new Tile.Builder();
	private static Tile[] tiles = new Tile[0]; 
	
	public static final int NOT_SET = -1;
	
	public static final Tile BLOCK =
			builder.setIndex(0)
			.setResourceId(R.drawable.tile)
			
			//Left line segment
			.addLineSegment(new LineSegment(
					new Vector2(0,0), //start
					new Vector2(0, BitmapConstants.TILE_HEIGHT), //end
							new Vector2(-1, 0))) //normal
							
			//Bottom line segment
			.addLineSegment(new LineSegment(
					new Vector2(0,0), //start
					new Vector2(BitmapConstants.TILE_WIDTH, 0), //end
							new Vector2(0, -1))) //normal
							
			//Right LineSegment
			.addLineSegment(new LineSegment(
					new Vector2(0,0), //start
					new Vector2(0, BitmapConstants.TILE_HEIGHT), //end
							new Vector2(1, 0))) //normal
							
			//Top LineSegment
			.addLineSegment(new LineSegment(
					new Vector2(0, BitmapConstants.TILE_HEIGHT), //start
					new Vector2(BitmapConstants.TILE_WIDTH, BitmapConstants.TILE_HEIGHT), //end
							new Vector2(1, 0))) //normal
			.build();
	
	public static final Tile RAMP_LEFT_1 = 
			builder.setIndex(1)
			.setResourceId(R.drawable.tile_left_tilt_1)
			.addLineSegment(new LineSegment(
					new Vector2(0,0), //start
					new Vector2(BitmapConstants.TILE_WIDTH, BitmapConstants.TILE_HEIGHT / 2), //end
							new Vector2(1, -2))) //normal
			.build();
	
	public static final Tile RAMP_LEFT_2 = 
			builder.setIndex(2)
			.setResourceId(R.drawable.tile_left_tilt_2)
			.addLineSegment(new LineSegment(
					new Vector2(0, BitmapConstants.TILE_HEIGHT / 2), //start 
					new Vector2(BitmapConstants.TILE_WIDTH, BitmapConstants.TILE_HEIGHT), //end
					new Vector2(1, -2))) //normal
			.build();

	public static Tile getTile(int index)
	{
		return tiles[index];
	}
	
	/**
	 * This is fired off every time the TileBuilder makes a new tile - this is all done before the level runs
	 * This builds the tile array that can be indexed by {@link TileWorld}
	 */
	public static void observe(Tile newTile) 
	{
		//If our array is not long enough, create a new one and copy elements over
		if(tiles.length <= newTile.index())
		{
			Tile[] newTiles = new Tile[newTile.index() + 1];
			
			for(int i = 0; i < tiles.length; i++)
				newTiles[i] = tiles[i];
			
			newTiles[newTile.index()] = newTile;
			tiles = newTiles;
		}
		else
			tiles[newTile.index()] = newTile;		
	}

	/**
	 * loads the bitmap in {@link tiles}
	 */
	public static void loadBitmaps(Context context, GL10 pGL) 
	{
		for(int i = 0; i < tiles.length; i++)
		{
			Tile tile = tiles[i];		
			if(tile == null)
				continue;
			
			GLHelper.loadBitmap(context, pGL, tile.getSprite());
		}
	}	
}
